package upgrade

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/upgrade"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var DevOtaStrategy = cUpgradeDev_ota_strategy{}

type cUpgradeDev_ota_strategy struct{}

// GetDevOtaStrategyList 获取列表
func (u *cUpgradeDev_ota_strategy) GetDevOtaStrategyList(ctx context.Context, req *upgrade.GetDevOtaStrategyListReq) (res *upgrade.GetDevOtaStrategyListRes, err error) {
	var reqData = new(model.GetDevOtaStrategyListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.UpgradeDevOtaStrategy().GetDevOtaStrategyList(ctx, reqData)
	res = new(upgrade.GetDevOtaStrategyListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetDevOtaStrategyById 获取指定ID数据
func (u *cUpgradeDev_ota_strategy) GetDevOtaStrategyById(ctx context.Context, req *upgrade.GetDevOtaStrategyByIdReq) (res *upgrade.GetDevOtaStrategyByIdRes, err error) {
	data, err := service.UpgradeDevOtaStrategy().GetDevOtaStrategyById(ctx, req.Id)
	res = new(upgrade.GetDevOtaStrategyByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddDevOtaStrategy 添加数据
func (u *cUpgradeDev_ota_strategy) AddDevOtaStrategy(ctx context.Context, req *upgrade.AddDevOtaStrategyReq) (res *upgrade.AddDevOtaStrategyRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaStrategyAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.UpgradeDevOtaStrategy().AddDevOtaStrategy(ctx, data)
	return
}

// EditDevOtaStrategy 修改数据
func (u *cUpgradeDev_ota_strategy) EditDevOtaStrategy(ctx context.Context, req *upgrade.EditDevOtaStrategyReq) (res *upgrade.EditDevOtaStrategyRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaStrategyEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.UpgradeDevOtaStrategy().EditDevOtaStrategy(ctx, data)
	return
}

// StopDevOtaStrategy
func (u *cUpgradeDev_ota_strategy) StopDevOtaStrategy(ctx context.Context, req *upgrade.StopDevOtaStrategyReq) (res *upgrade.StopDevOtaStrategyRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}

	err = service.UpgradeDevOtaStrategy().StopDevOtaStrategy(ctx, req.Id, req.Active)
	return
}

// DeleteDevOtaStrategy 删除数据
func (u *cUpgradeDev_ota_strategy) DeleteDevOtaStrategy(ctx context.Context, req *upgrade.DeleteDevOtaStrategyReq) (res *upgrade.DeleteDevOtaStrategyRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.UpgradeDevOtaStrategy().DeleteDevOtaStrategy(ctx, req.Ids)
	return
}
